﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EasyMvvm.Tests
{
    [TestClass]
    public class ServiceLocatorTest
    {
        interface ITestService
        {
            void Test();

        }


        class TestService : ITestService
        {
            public bool HasRun;

            public void Test()
            {
                HasRun = true;
            }
        }

        [TestMethod]
        public void TestRegister()
        {
            TestService service = new TestService();
            ServiceLocator.Instance.Register<ITestService>(service);
            ServiceLocator.Instance.Get<ITestService>(true).Test();
            Assert.IsTrue(service.HasRun);
            Assert.IsTrue(ServiceLocator.Instance.Unregister<ITestService>(service));
        }

        [TestMethod]
        public void TestMethodRegister()
        {
            ServiceLocator.Instance.Register<ITestService>(new Func<ITestService>(GetNewService));
            this.HasRun = false;
            ServiceLocator.Instance.Get<ITestService>(true).Test();
            Assert.IsTrue(this.HasRun);
            Assert.IsTrue(ServiceLocator.Instance.Unregister<ITestService>(new Func<ITestService>(GetNewService)));
        }

        [TestMethod]
        public void TestThrow()
        {
            bool gotException = false;
            try
            {
                ServiceLocator.Instance.Get<IDisposable>(true);
            }
            catch (ArgumentException)
            {
                gotException = true;
            }
            Assert.IsTrue(gotException);
        }

        private bool HasRun = false;
        private ITestService GetNewService()
        {
            HasRun = true;
            return new TestService();

        }
    }
}
